﻿/* <copyright file="OPWorkEfficiencyQuery.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Li Dawei</author>
 * <summary> 
 *          OP别 稼动率 查询
 *          作成日：06/13/2010
 * </summary>
 */ 

namespace Orid.Management.Statistics.OperatorUnitMangement.Command.OP
{
    using System;
    using System.Collections.ObjectModel;
    using System.Text;
    using System.Windows;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFLog4Ref;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.OPQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// OP 稼动率查询 实现ICommand接口
    /// </summary>
    public class OPWorkEfficiencyQuery : ICommand
    {
        /// <summary>
        /// 稼动查询服务定义 wcfOPWorkEfficiencyQueryClient
        /// </summary>
        private WCFOPCorrelationClient wcfOPWorkEfficiencyQueryClient;

        /// <summary>
        /// 查询控件对象 container
        /// </summary>
        private SelAnyContainer container;

        /// <summary>
        /// 错误提示对话框 errorMessbox
        /// </summary>
        private childWindow.MessageBox errorMessbox = new childWindow.MessageBox();
      
        /// <summary>
        /// 日志服务定义 wcfLogClient
        /// </summary>
        private WCFLog4Client wcfLogClient;

        /// <summary>
        /// 构造函数 OPWorkEfficiencyQuery
        /// </summary>
        /// <param name="container">查询控件 container</param>
        public OPWorkEfficiencyQuery(Common.SearchCtrl.SelAnyContainer container)
        {
            this.container = container;

            this.InitField();
        }

        /// <summary>
        /// 查询完成事件 ExecOver
        /// </summary>
        public event EventHandler<SelectOPWorkEfficiencyCompletedEventArgs> ExecOver;

        /// <summary>
        /// 查询开始事件 ExecBegin
        /// </summary>
        public event EventHandler<EventArgs> ExecBegin;

        #region ICommand 成员

        /// <summary>
        /// 稼动查询命令 Exec
        /// </summary>
        public void Exec()
        {
            // 传送到服务端的参数列表
            ObservableCollection<WCFServers.OPQueryRef.WhereParametersForWorkEff> clientWhereList = new ObservableCollection<WCFServers.OPQueryRef.WhereParametersForWorkEff>();

            // this.tbOPID.Text dpDateFrom dpDateTo内容必选的判断
            // 日期不能相同
            // 开始日期不能小于结束日期
            int error = FunctionClass.IsSelectDo(
                this.container.dpDateFrom, 
                this.container.dpDateTo, 
                this.container.tbOPID,
                this.errorMessbox);

            if (error == 0)
            {
                this.container.btnSelect.IsEnabled = true;
                return;
            }

            try
            {
                try
                {
                    //--30/05/2010--修改--服务端改为存储过程，传送数据不加单引号
                    #region  组合条件
                    // 组合客户端的WHERE条件
                    WCFServers.OPQueryRef.WhereParametersForWorkEff fitWhereData = new WCFServers.OPQueryRef.WhereParametersForWorkEff();

                    // 日期的判断条件
                    // 日期返回空值用"null"
                    if (!String.IsNullOrEmpty(this.container.dpDateFrom.Text))
                    {
                        fitWhereData.WhereentryDateFrom = this.container.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        fitWhereData.WhereentryDateFrom = "null";
                    }

                    if (!String.IsNullOrEmpty(this.container.dpDateTo.Text))
                    {
                        fitWhereData.WhereentryDateTo = this.container.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        fitWhereData.WhereentryDateTo = "null";
                    }

                    // 判断是ALL还是部分OPID
                    if (this.container.tbOPID.Tag.ToString().Contains("ALL"))
                    {
                        // 选择所有OP或是UNIT
                        StringBuilder sb = new StringBuilder();
                        if (ClientDataList.CheckOpUnitList.Count > 0)
                        {
                            foreach (string s in ClientDataList.CheckOpUnitList)
                            {
                                // 添加引号和逗号
                                // sb.Append("'" + s + "'" + ",");
                                // 添加逗号
                                sb.Append(s + ",");
                            }

                            // 去除尾部多余逗号
                            fitWhereData.WhereUnit = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                        }

                        fitWhereData.WhereLoginName = null;
                    }
                    else
                    {
                        // 选择部分OP
                        // 已加完逗号，不用再转换
                        fitWhereData.WhereLoginName = FunctionClass.MyIsNullOrEmpty(this.container.tbOPID.Text.Trim().ToString(), null);
                        fitWhereData.WhereUnit = null;
                    }

                    // 判断稼动率的类型
                    if (this.container.cbWorkEfficiency.SelectedIndex != -1)
                    {
                        //fitWhereData.WhereStatsticClass = this.container.cbWorkEfficiency.SelectedItem.ToString();
                        fitWhereData.WhereStatsticClass = this.container.cbWorkEfficiency.SelectionBoxItem.ToString();
                    }
                    else
                    {
                        fitWhereData.WhereStatsticClass = null;
                    }

                    // 将类增加到  参数列表中
                    clientWhereList.Add(fitWhereData);

                    #endregion  组合条件
                }
                catch (Exception ex)
                {
                    this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " OP稼动率查询接口实现类 OPWorkEfficiencyQuery 转换查询条件出错" + ex.Message.ToString());

                    Common.FunctionList.FunctionClass.ShowMessageBox(
                       this.errorMessbox,
                       1,
                       "转换查询条件出错",
                       ex.Message.ToString(),
                       Colors.Gray,
                       true,
                       Visibility.Visible,
                       Visibility.Collapsed);
                    return;
                }

                this.OnExecBegin(null, null);

                // 调用 稼动率的服务端方法
                this.wcfOPWorkEfficiencyQueryClient.SelectOPWorkEfficiencyAsync(clientWhereList);
                clientWhereList.Clear();
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " OP稼动率查询接口实现类 OPWorkEfficiencyQuery调用SelectOPWorkEfficiencyAsync服务出错" + ex.Message.ToString());
               
                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "调用SelectOPWorkEfficiencyAsync服务出错",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }
      
        #endregion

        /// <summary>
        /// 查询完成事件 OnExecover
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of SelectOPWorkEfficiencyCompletedEventArgs</param>
        protected void OnExecover(object sender, SelectOPWorkEfficiencyCompletedEventArgs e)
        {
            if (this.ExecOver != null)
            {
                this.ExecOver(this, e);
            }
        }

        /// <summary>
        /// 查询开始事件 OnExecBegin
        /// </summary>
        /// <param name="sender">Sender of Object</param>
        /// <param name="e">e type of EventArgs</param>
        protected void OnExecBegin(object sender, EventArgs e)
        {
            if (this.ExecBegin != null)
            {
                this.ExecBegin(this, e);
            }
        }
        
        /// <summary>
        /// 初始化变量 InitField
        /// </summary>  
        private void InitField()
        {
            this.wcfOPWorkEfficiencyQueryClient = Wcf.Service.OPWorkEfficiencyQuery;
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfLogClient = Wcf.Service.Log4;

            // 稼动率查询
            this.wcfOPWorkEfficiencyQueryClient.SelectOPWorkEfficiencyCompleted += 
                new EventHandler<SelectOPWorkEfficiencyCompletedEventArgs>(this.OnExecover);
        }
    }
}
